package com.example.administrator.yuejian35.Home.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.yuejian35.Home.HomeModel;
import com.example.administrator.yuejian35.Home.adapter.MasterAdapter;
import com.example.administrator.yuejian35.Home.bean.Master;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.button.ShineButton;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class MasterFragment extends Fragment {

    private MasterAdapter adapter;
    private RecyclerView rv;

    private ShineButton dianzan;

    public MasterFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rv = (RecyclerView) view.findViewById(R.id.rv_master);

        HomeModel homeModel = new HomeModel();
        Call call = homeModel.masterlist();

        call.enqueue(new Callback<List<Master>>() {
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response) {
                if(getActivity()!=null){

                    List<Master> masterlist = response.body();
                    adapter = new MasterAdapter(getActivity(),masterlist);

                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

                    rv.setLayoutManager(linearLayoutManager);
                    rv.setAdapter(adapter);
                    rv.setItemAnimator(new DefaultItemAnimator());

                }

                //Toast.makeText(getActivity(),"查询成功！！！",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<List<Master>> call, Throwable t) {
                Toast.makeText(getActivity(),"查询失败！！！",Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_master, container, false);
    }

}
